#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include <string.h>

#include "hj212_client.h"
#include "hj212_client_port.h"

#include <windows.h>

#include <pthread.h>

// #pragma comment(lib, "ws2_32.lib")

#define HJ_LOG_TAG  "hj212.main"        // 日志标签
#define HJ_LOG_LVL HJ_DBG_LOG           // 日志等级
#include "hjdebug.h"


#define sleep Sleep

struct hj212_client *hj212_simulator_create(int id);

#if 1

struct terminal_cmd
{
    const char *cmd;
    // const char *describe;
    int (*cmd_func)(int argc, char **argv);
};

struct hj212_client *client_simulator = NULL;

void *client_entry(void *param)
{
    struct hj212_client *client = param;
    hj212_client_start(client);
    while (1)
    {
        hj212_handle(client);
        sleep(50);
    }
    return NULL;
}
int simulator_test(int argc, char **argv)
{
    char buffer[1024];
    memset(buffer, 0, sizeof(buffer));
    strcat(buffer, argv[0]);
    for (int i = 1; i < argc; i++)
    {
        strcat(buffer, " ");
        strcat(buffer, argv[i]);
    }
    HJ_LOG_I("test-input:(%s)", buffer);
    return 0;
}
int simulator_quit(int argc, char **argv)
{
    exit(0);
    return 0;
}
int simulator_push_enable(int argc, char **argv)
{
    if(argc == 2)
    {
        int enable = !!atoi(argv[1]);
        for(int i = 0; i < HJ212_MSG_TYPE_NUM; i++)
        {
            hj212_client_pushstate(client_simulator, i, enable);
        }
    }
    return 0;
}
int simulator_updata(int argc, char **argv)
{
    if(argc == 2)
    {
        int type = atoi(argv[1]);
#if 0
        time_t end = hj212_client_current_timestamp(client_simulator);
        time_t begin = (end / 3600 - 1) * 3600;
#endif  
        time_t end = hj212_client_current_timestamp(client_simulator) / 3600 * 3600;
        time_t begin = end - 3600;
        hj212_client_updata_begin(client_simulator, type, begin, end, 0);
    }
    return 0;
}


const struct terminal_cmd ter_cmd[] =
{
    {
        .cmd = "test",
        .cmd_func = simulator_test,
    },
    {
        .cmd = "quit",
        .cmd_func = simulator_quit,
    },
    {
        .cmd = "exit",
        .cmd_func = simulator_quit,
    },
    {
        .cmd = "pushenable",
        .cmd_func = simulator_push_enable,
    },
    {
        .cmd = "updata",
        .cmd_func = simulator_updata,
    }
};

int main(void)
{
    WSADATA Ws;
    WSAStartup(MAKEWORD(2, 2), &Ws);

    HJ_LOG_I("System start");

    client_simulator = hj212_simulator_create(0);

    pthread_t client_thread;
    pthread_create(&client_thread, NULL, client_entry, client_simulator);

    while (1)
    {
        char command[1024];
        char *argv[100];
        memset(command, 0, sizeof(command));
        printf("/> ");
        gets(command);
        if (strlen(command) > 0)
        {
            HJ_LOG_I("input (%s)", command);
            int argc = 0;
            for (char *p = strtok(command, " \t"); p != NULL; p = strtok(NULL, " \t"))
            {
                argv[argc++] = p;
            }
            int i;
            for (i = 0; i < sizeof(ter_cmd) / sizeof(*ter_cmd) && argc > 0; i++)
            {
                if (!strcmp(ter_cmd[i].cmd, argv[0]))
                {
                    int result = ter_cmd[i].cmd_func(argc, argv);
                    if (result)
                    {
                        HJ_LOG_W("command result = %d", result);
                    }
                    break;
                }
            }
            if (i >= sizeof(ter_cmd) / sizeof(*ter_cmd))
            {
                HJ_LOG_W("no command");
            }
        }
    }
    return 0;
}

#else

int main(void)
{
    WSADATA Ws;
    WSAStartup(MAKEWORD(2, 2), &Ws);

    struct hj212_client *client = hj212_simulator_create(0);


    printf("Sec = %d\n", (int)hj212_client_current_timestamp(client));
    printf("mSec = %d\n", (int)hj212_client_current_subsec(client).tv_usec);


    printf("dev_open = %d\n", hj212_client_dev_open(client, 0));

    sleep(1000);
    printf("dev_send = %d\n", hj212_client_dev_send(client, (void *)"dsfasfdssdfjkojfds", strlen("dsfasfdssdfjkojfds")));
    sleep(1000);
    printf("dev_send = %d\n", hj212_client_dev_send(client, (void *)"dsfasfdssdfjkojfds", strlen("dsfasfdssdfjkojfds")));
    sleep(1000);

    printf("dev_close = %d\n", hj212_client_dev_close(client));


    printf("\n\033[31;1m字体颜色\033[0m测试文字\n");
    printf("\e[30m低亮黑色文字\033[0m\t\033[30;1m高亮黑色文字\033[0m\n");
    printf("\x1b[31m低亮红色文字\033[0m\t\033[31;1m高亮红色文字\033[0m\n");
    printf("\027[32m低亮绿色文字\033[0m\t\033[32;1m高亮绿色文字\033[0m\n");
    printf("\033[33m低亮黄色文字\033[0m\t\033[33;1m高亮黄色文字\033[0m\n");
    printf("\033[34m低亮蓝色文字\033[0m\t\033[34;1m高亮蓝色文字\033[0m\n");
    printf("\033[35m低亮紫色文字\033[0m\t\033[35;1m高亮紫色文字\033[0m\n");
    printf("\033[36m低亮浅蓝文字\033[0m\t\033[36;1m高亮浅蓝文字\033[0m\n");
    printf("\033[37m低亮白色文字\033[0m\t\033[37;1m高亮白色文字\033[0m\n");

    HJ_LOG_D("HJ_LOG_D %.4f", 1.01);
    HJ_LOG_I("HJ_LOG_I %.4f", 1.01);
    HJ_LOG_W("HJ_LOG_W %.4f", 1.01);
    HJ_LOG_E("HJ_LOG_E %.4f", 1.01);
    HJ_LOG_HERE();

    uint8_t buffer[256 + 16];

    HJ_LOGH_D(buffer, sizeof(buffer), "HJ_LOG_D %.4f", 1.02);
    HJ_LOGH_I(buffer, sizeof(buffer), "HJ_LOG_I %.4f", 1.02);
    HJ_LOGH_W(buffer, sizeof(buffer), "HJ_LOG_W %.4f", 1.02);
    HJ_LOGH_E(buffer, sizeof(buffer), "HJ_LOG_E %.4f", 1.02);

    // HJ_ASSERT(0);

    return 0;
}
#endif










